import {getStorage} from "@/utils/storage";
import {invertTree} from "@/utils/common";

const tabState = {
    isCollapse: false,
    menu: [],
    tab: []
}
const tabGetters = {
    isCollapse: (state: any) => state.isCollapse,
    noChildren: (state: any) => state.menu.filter((item: any) => !item.children),
    hasChildren: (state: any) => state.menu.filter((item: any) => item.children)
}
const tabMutations = {
    GET_MENU(state: any) {
        let list = JSON.parse(JSON.stringify(getStorage("routes")))
        for (let i = 0; i < list.length; i++) {
            if (i < 0) i = 0
            if (list[i].pid==null || !list[i].meta.isMenu) {
                list.splice(i, 1)
                i--
            }
        }
        state.menu = invertTree(list, "id", "pid", "children")
    },
    changeCollapse(state: any) {
        state.isCollapse = !state.isCollapse
    }

}
const tabActions = {}

export {
    tabState,
    tabGetters,
    tabMutations,
    tabActions
}
